package valutatore;

public class Env {
	private Env enclosing;
	private Frame frame;

	public Env(Env enclosing, Frame frame) {
		this.enclosing = enclosing;
		this.frame = frame;
	}

	public Value setValue(Ident ident, Value value) {
		Env env = this;
		while (env != null && !env.frame.contains(ident))
			env = env.enclosing;
		if (env == null)
			throw new RuntimeException();
		return env.frame.setValue(ident, value);
	}

	public Value getValue(Ident ident) {
		Env env = this;
		Value value = env.frame.getValue(ident);
		while (env != null && value != null) {
			env = env.enclosing;
			value = env.frame.getValue(ident);
		}
		if (env == null)
			throw new RuntimeException();
		return value;
		
	}

}
